package com.zjjh.gateway.handler;

import com.zjjh.gateway.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Slf4j
public class JsonExceptionHandler implements ErrorWebExceptionHandler {

    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
        ServerHttpResponse response = exchange.getResponse();
        ex.printStackTrace();
        DataBuffer dataBuffer = ResponseUtil.getResponseBuf(exchange.getResponse(), ex);
        return response.writeWith(Mono.just(dataBuffer));
    }

}
